home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3521 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  86 lines

  1. Path: news.NetVision.net.il!news
  2. From: Moti Saadon <moti@netmanage.co.il>
  3. Newsgroups: comp.lang.c++
  4. Subject: [] overloding vc4.0
  5. Date: Wed, 24 Jan 96 16:41:53 PDT
  6. Organization: NetVision LTD.
  7. Message-ID: <NEWTNews.822530713.12255.moti@motisaad.netmanage.co.il>
  8. NNTP-Posting-Host: motisaan.netmanage.co.il
  9. Mime-Version: 1.0
  10. Content-Type: TEXT/PLAIN; charset=US-ASCII
  11. X-Newsreader: NEWTNews & Chameleon -- TCP/IP for MS Windows from NetManage
  12.  
  13.  
  14. Why can't I compile this code at vc++4.0?
  15. Thanks.
  16.  
  17. //----------------------------begin code-----------------
  18.  
  19. //temp.cpp
  20.  
  21. #include <stdio.h>
  22. #include <iostream.h>
  23.  
  24. class Tclass{
  25. public:
  26.     int temp;
  27. };
  28.  
  29.  
  30. //Range exeption
  31. class Range {
  32.     int low, hi, badIndex;
  33. public:
  34.     Range (int l, int h, int b)
  35.     { low = l; hi = h; badIndex = b; }
  36.     void display()
  37.     { printf("\nRange error {%d, %d} :%d",
  38.     low, hi, badIndex); }
  39. };
  40.  
  41.  
  42. template <class Type>
  43. class Array {
  44.     Type *array;
  45.     int size;    //size of the array
  46. public:
  47.     Array (int sz)
  48.     { array = new Type[size = sz];}
  49.     ~Array () {delete [] array;}
  50.     Type &operator [] (int i);
  51. };
  52.  
  53. //now comes the definition of the subscript operator
  54. template <class Type>
  55. Type &Array::operator[] (int i)
  56. {
  57.     if (i<0 || i>=size)
  58.         throw Range(0, size-1, i);
  59.     return array[i];
  60. }
  61.  
  62. void func(Array<Tclass> &arr)
  63. {
  64.     try {
  65.         for (int i=0; i<5; i++)
  66.             //do some work with arr[i]
  67.             arr[i].temp = 0;                
  68.     
  69.     }
  70.         catch (Range &r) {
  71.         // report error condition
  72.             r.display(); 
  73.     }
  74. }
  75.  
  76.  
  77. void main()
  78. {
  79.     //define Array of size 50
  80.     Array<Tclass> array(3);
  81.     func(array);    
  82. }
  83.  
  84. //------------------------end of code ---------------------
  85.  
  86.